package com.devshephard.jucks.threadSafeCollections.array;


import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class copyAndWriteList {

    public static void main(String[] args) throws InterruptedException {

        /**
         * 线程不安全的
         */
        List<String> list = new ArrayList<>();
        Set<String> stringSet = new HashSet<>();

        /**
         * 线程安全的
         */
        List<String> listSync = Collections.synchronizedList(new ArrayList<>());
        List<String> listCopyAndWrite = new CopyOnWriteArrayList<>();
        Set<String> CopyAndWriteSet = new CopyOnWriteArraySet<>();

     /*   for (int i = 0; i < 10; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(list);
            },String.valueOf(i)).start();
        }*/

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                listCopyAndWrite.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(listCopyAndWrite);
            },String.valueOf(i)).start();
        }

          for (int i = 0; i < 10; i++) {
            new Thread(()->{
                CopyAndWriteSet.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(CopyAndWriteSet);
            },String.valueOf(i)).start();
        }

        Thread.sleep(2000);
        System.out.println("最终listCopyAndWrite结果：" + listCopyAndWrite);
        System.out.println("最终CopyAndWriteSet结果：" + CopyAndWriteSet );




    }

}
